package com.xnjx.controller;

import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.net.URL;

public class TestOpenCV {
    public static void main(String[] args) {
        URL url = null;
        String os = System.getProperty("os.name");
//        if(os.startsWith("Linux")){
//            url = ClassLoader.getSystemResource("opencvlib/libopencv_java452.so");
//        }else{
//            url = ClassLoader.getSystemResource("D:\\file\\opencv_java460.dll");
//        }
        System.load("D:\\file\\opencv_java460.dll");
        String path = "D:\\file\\haarcascade_frontalface_alt.xml";
        String imgPath = "D:\\file\\24d11hscdn.webp";
        CascadeClassifier faceDetector = new CascadeClassifier(path);
        Mat image = Imgcodecs.imread(imgPath);
        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image,faceDetections);
        System.out.println(String.format("Detected %s faces",faceDetections.toArray().length));
        for(Rect rect:faceDetections.toArray()){
            Imgproc.rectangle(image,new Point(rect.x,rect.y),new Point(rect.x+rect.width,rect.y+rect.height),new Scalar(0,255,0));
        }
        String out = "D:\\file\\output.png";
        System.out.println(String.format("Writing %s",out));
        boolean flag = Imgcodecs.imwrite(out,image);
        System.out.println("done->"+flag);
    }
}
